package com.jdbctemplate.controller;

import com.jdbctemplate.pojo.StudentInfo;
import com.jdbctemplate.service.StudentInfoService;
import com.jdbctemplate.service.impl.StudentInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 从卿
 * @create 2022/5/31-20:54
 */
@Controller
public class StudentInfoController {
    @Autowired
    private StudentInfoService studentInfoService;

    /**
     * 插入与查询学生操作
     */
    @RequestMapping(value = "/insertStudent.d", method = RequestMethod.GET)
    public ModelAndView insertStudent(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String address = request.getParameter("address");
        StudentInfo student = new StudentInfo(name, age, address);
        int i = studentInfoService.addStudent(student);
        if (i > 0) {
            mv.addObject("suceess", "插入成功");
            mv.setViewName("jtStudentInsert");
        }

        return mv;
    }

    /**
     * 预更新学生操作
     *
     * @return
     */
    @RequestMapping(value = "/preUpdateStudent.d", method = RequestMethod.GET)
    public ModelAndView preUpdateStudent(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        String name = request.getParameter("name");
        StudentInfo stu = studentInfoService.preUpdateStu(name);
        mv.addObject("stu", stu);
        mv.setViewName("jtStudentUpdate");
        return mv;
    }

    /**
     * 更新学生操作
     *
     * @return
     */
    @RequestMapping(value = "/updateStudent.d", method = RequestMethod.GET)
    public ModelAndView updateStudent(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        StudentInfo stu = new StudentInfo(name, address);
        int i = studentInfoService.updateStu(stu);
        if (i > 0) {
            mv.addObject("stu", stu);
            mv.addObject("yes", "修改地址成功");
            mv.setViewName("jtStudentUpdate");
        }
        return mv;


    }
    /**
     * 查询学生操作
     *
     * @return
     */
    @RequestMapping(value = "/selectStudent.d", method = RequestMethod.GET)
    public ModelAndView selectStudent(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();

        List<StudentInfo> studentList = studentInfoService.showStu();
        mv.addObject("studentList", studentList);
        mv.setViewName("jtStudentSelect");
        return mv;


    }
}
